package com.youlai.boot.system.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.ExamQuestionMapper;
import com.youlai.boot.system.service.ExamQuestionService;
import com.youlai.boot.system.model.entity.ExamQuestion;
import com.youlai.boot.system.model.form.ExamQuestionForm;
import com.youlai.boot.system.model.query.ExamQuestionQuery;
import com.youlai.boot.system.model.vo.ExamQuestionVO;
import com.youlai.boot.system.converter.ExamQuestionConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 试题列表服务实现类
 *
 * @author youlaitech
 * @since 2025-07-07 09:05
 */
@Service
@RequiredArgsConstructor
public class ExamQuestionServiceImpl extends ServiceImpl<ExamQuestionMapper, ExamQuestion> implements ExamQuestionService {

    private final ExamQuestionConverter examQuestionConverter;

    /**
    * 获取试题列表分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<ExamQuestionVO>} 试题列表分页列表
    */
    @Override
    public IPage<ExamQuestionVO> getExamQuestionPage(ExamQuestionQuery queryParams) {
        Page<ExamQuestionVO> pageVO = this.baseMapper.getExamQuestionPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取试题列表表单数据
     *
     * @param id 试题列表ID
     * @return 试题列表表单数据
     */
    @Override
    public ExamQuestionForm getExamQuestionFormData(Long id) {
        ExamQuestion entity = this.getById(id);
        return examQuestionConverter.toForm(entity);
    }
    
    /**
     * 新增试题列表
     *
     * @param formData 试题列表表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveExamQuestion(ExamQuestionForm formData) {
        ExamQuestion entity = examQuestionConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新试题列表
     *
     * @param id   试题列表ID
     * @param formData 试题列表表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateExamQuestion(Long id,ExamQuestionForm formData) {
        ExamQuestion entity = examQuestionConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除试题列表
     *
     * @param ids 试题列表ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteExamQuestions(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的试题列表数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
